

let activity_tag = 'jd'

let request_key = activity_tag+'_award_url'
let request_user_key = activity_tag+'_cookies'
let skill_count_key = activity_tag+'_award_count'
let request_log_key = activity_tag+'_award_log'
let skill_name = 'skill_name'
let skill_result = false
let request_value = ""
let request_log = ""
let request_user = ""

function rewrite_body(body) {
    console.log(body);
    if (body.subCodeMsg){
        body.subCodeMsg = "成功抓到请求";
    }
}  

function resp_case(count,res) {
    console.log('请求结果：');
    console.log(res.headers.date);
    console.log(res.data);
    if(res.data.subCodeMsg.includes("领取成功")){
        let skill_name = $store.get(skill_name);
        $feed.push(skill_name+"秒杀结果：", res.data.subCodeMsg)
       
    }
    // else if(res.data.subCodeMsg.includes("已抢完")){
    //     $feed.push(skill_name+"秒杀结果：", res.data.subCodeMsg)
    // }
    // else{
    //     axios_skill(count)
    // }
    if(!request_log.includes(res.data.subCodeMsg)){
        request_log= request_log + res.headers.date+":"+res.data.subCodeMsg +"\n"
        $store.set(request_log_key, request_log) 
    }
    
  
}
function error_case(count,e) {
    console.log(e.message)
    // axios_skill(count)
}

//上面是暂时需要适配的部分
!(async () => {
    if (typeof $request !== "undefined") {
  console.log(`开始获取请求参数`);
    $done()
}else {
    console.log(`开始执行请求`);
    request_log  = $store.get(skill_name)+'\n';
    $store.set(request_log_key, request_log) 

    request_value = $store.get(request_key);
    request_user = $store.get(request_user_key);
    if(request_value&&request_value!=""&&request_user&&request_user!=""){
        //  $store.delete没有暴露出来，无法使用，兼容下
        // $store.set(request_key, "")  
        let count = $store.get(skill_count_key);
        if (!count){
            $store.set(skill_count_key, 1)  
            count = 1
        }
        
        // setTimeout(axios_skill,700,count);
        setTimeout(axios_skill,980,count);
        // setTimeout(axios_skill,1450,count);
        setTimeout(axios_skill,1950,count);
        // setTimeout(axios_skill,2450,count);
        setTimeout(axios_skill,2950,count);
        // setTimeout(axios_skill,3450,count);
        setTimeout(axios_skill,3950,count);
        // setTimeout(axios_skill,4950,count);
        axios_skill(count);
        
    }else{
        console.log(request_key+` ，或者 `+request_user_key+` 不存在`);
    }
    }
    })()
   .catch((e) =>console.log(e))
   .finally(() =>$done())

function axios_skill(count){
    if(count>0){
        console.log(`执行请求：`);
        count--;
        $axios({
            "method": "GET",
            "headers": {
              "Cookie": request_user,
            },
            "url":request_value,
          })
        .then(res=>resp_case(count,res))
        .catch(e=>error_case(count,e));  
    }
  
}


function get_request_key() {
    $store.set(request_key, $request) 
}
